﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using Runda.CMS.Common.Utility;
using System.Collections.Generic;
using Telerik.Web.UI;
using System.Text;

namespace Runda.CMS.Portal.SalesManage
{
    public partial class MaintainCompanyInstrumentCost : BasePage
    {
        public string SortString
        {
            get
            {
                if (ViewState["SortString"] == null)
                {
                    ViewState["SortString"] = "instrumentcode";
                }

                return ViewState["SortString"].ToString();
            }
            set { ViewState["SortString"] = value; }
        }

        

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable dt = new BudgetBll().Companylist(base.CurrentUserID.ToString());
                ddlCompany.DataSource = dt;
                ddlCompany.DataTextField = "Departmentname";
                ddlCompany.DataValueField = "Departmentname";
                ddlCompany.DataBind();
                ddlCompany.Items.Add(new ListItem("--不限--", ""));
                ddlCompany.SelectedValue = "";

                LoadRadTree("");
                BindGrid(1);
               
                
            }
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGrid(1);
            
        }

        protected void gridAccessoryList_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Item || e.Item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem)
            {
                LinkButton lnkbtnDelete = e.Item.FindControl("lnkbtnDelete") as LinkButton;
                if (lnkbtnDelete != null)
                {
                    //DataRowView dv = e.Item.DataItem as DataRowView;
                    //if (new BudgetBll().GetBudgetSubmitStatus(ddlBudgetYear.SelectedValue.ToString(), dv["ProductManager"].ToString()) > 0 || !base.CheckUserHasFuction(1, "Func10011"))
                    //{ lnkbtnDelete.Visible = false; }

                    //lnkEdit.NavigateUrl = "~/Budget/SalesbudgetDetail.aspx?AssistID=" + dv["AssistID"].ToString();
                    lnkbtnDelete.OnClientClick = "return OnDelConfirm();";
                }
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindGrid(AspNetPager1.CurrentPageIndex);
            //Calculate();
        }

        protected void btnRefersh_Click(object sender, EventArgs e)
        {
            BindGrid(AspNetPager1.CurrentPageIndex);
            //Calculate();
        } 

        private void BindGrid(int pageindex)
        {
            string sb = getWhereClause();

            int RecordCount = 0;
            DataTable dt = new BillBll().QueryCompanyInstrumentCostList( sb, SortString, AspNetPager1.PageSize, pageindex, out RecordCount);

            AspNetPager1.RecordCount = RecordCount;
            gridAccessoryList.DataSource = dt;
            gridAccessoryList.DataBind();
        }

        protected void gridAccessoryList_SortCommand(object source, Telerik.WebControls.GridSortCommandEventArgs e)
        {
            string OrderBy = e.SortExpression + " " + (e.NewSortOrder == Telerik.WebControls.GridSortOrder.Ascending ? "asc" : "desc");
            SortString = OrderBy;
            BindGrid(1);
            //Calculate();
            

        }

        private string getWhereClause()
        { 
            StringBuilder sb = new StringBuilder();

            string categoryCode = "";

            SystemBll bll = new SystemBll();

            if (Text1.Value != "")
            {
                Guid ID = new Guid(Text1.Value);
                CtrtInstrumentCategoryEntity instrmentEntity = bll.Get<CtrtInstrumentCategoryEntity>(ID);
                if (instrmentEntity != null)
                {
                    categoryCode = instrmentEntity.categoryCode;
                }
                sb.Append(" and Categorycode  like '").Append(categoryCode).Append("%'");
            }

            if (ddlCompany.SelectedValue != "")
            {
                sb.Append(" and CompanyName ='" + ddlCompany.SelectedValue.ToString() + "'");
            }


            if (txtInstrumentName.Text.Trim() != "")
            {
                sb.Append(" and InstrumentName  like '%").Append(txtInstrumentName.Text.Trim()).Append("%'");
            }

            if (txtInstrumentCode.Text.Trim() != "")
            {
                sb.Append(" and InstrumentCode  like '").Append(txtInstrumentCode.Text.Trim()).Append("%'");
            }

            switch (ddlScope.SelectedValue)
            {
                case "0":
                    break;
                case "1":
                    sb.Append(" and CostLow is not null and CostLow !=0");
                    break;
                case "2":
                    sb.Append(" and categoryName not like '%折扣' and  (CostImport is null or CostImport=0 or CostLow is null or CostLow=0)");
                    break;
                case "3":
                    sb.Append(" and categoryName not like '%折扣' and (CostLow is null or CostLow=0) and exists (select 1 from Cms_BG_Bill b where b.cInvCode = instrumentcode  and b.BillUnit=CompanyName)");
                    break;
                case "4":
                    sb.Append(" and categoryName not like '%折扣' and (CostImport is null or CostImport=0) and exists (select 1 from Cms_BG_Bill b where b.cInvCode = instrumentcode  and b.BillUnit=CompanyName)");
                    break;
                
            }

            
            return sb.ToString();
        }

        protected void btnExportExcel_Click(object sender, EventArgs e)
        {
            string Whereclause = getWhereClause();

            BillBll bll = new BillBll();

            DataTable dt = bll.CompanyInstrumentList_export(Whereclause);

            Export.Export2Excel(dt, "存货单价明细", Page.Response);

        }

        protected void lnkbtnSubmit_Click(object sender, EventArgs e)
        {

            

        }

        protected void lnkbtnCheck_Click(object sender, EventArgs e)
        {

            BudgetBll bll = new BudgetBll();
            //bll.CheckBudget(ddlBudgetYear.SelectedValue.ToString(), base.CurrentUserEntity.UserName);
        }

        private void LoadRadTree(string categoryCode)
        {
            string categoryGUID = "";
            //List<CtrtInstrumentCategoryEntity> categoryEntitys = new CtrtInstrumentCategoryBll().GetAll<CtrtInstrumentCategoryEntity>(" and parentGUID is null", "Order By categoryCode"); 
            DataTable dt = null;
            CtrtInstrumentCategoryBll bll = new CtrtInstrumentCategoryBll();
            dt = bll.GetInstrumentCategory("");//  and SUBSTRING(categorycode,1,2) in('01', '02')


            RadTreeView RadTreeProdType = RadComboBox_ProdType.Items[0].FindControl("RadTreeView_ProdType") as RadTreeView;
            //HtmlInputHidden Hidden_ProdTypeID = Page.FindControl("Text1") as HtmlInputHidden;
            //HtmlInputText txtSelectType = Page.FindControl("txt_SelectType") as HtmlInputText;
            if (RadTreeProdType != null)
            {
                RadTreeProdType.DataFieldID = "categoryGUID";
                RadTreeProdType.DataTextField = "categoryName";
                RadTreeProdType.DataValueField = "categoryGUID";
                RadTreeProdType.DataFieldParentID = "parentGUID";
                RadTreeProdType.DataSource = dt;
                RadTreeProdType.DataBind();
            }
            if (categoryCode != "")
            {
                Text1.Value = categoryCode;
                List<CtrtInstrumentCategoryEntity> categoryEntitys = new CtrtInstrumentCategoryBll().GetAll<CtrtInstrumentCategoryEntity>(" and parentGUID is null", "Order By categoryCode");

                if (categoryEntitys != null && categoryEntitys.Count > 0)
                {
                    categoryGUID = categoryEntitys[0].categoryCode;
                }

                if (categoryGUID != "")
                {
                    txt_SelectType.Value = RadTreeProdType.FindNodeByValue(categoryGUID).Text;

                    RadComboBox_ProdType.SelectedItem.Value = RadTreeProdType.FindNodeByValue(categoryGUID).Text;
                    RadComboBox_ProdType.SelectedItem.Text = RadTreeProdType.FindNodeByValue(categoryGUID).Text;
                    RadComboBox_ProdType.Text = RadTreeProdType.FindNodeByValue(categoryGUID).Text;
                }
            }

        }

        protected void gridAccessoryList_ItemCreated(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Header)
            {
                //获取表头所在行的所有单元格
                TableCellCollection tcHeader = e.Item.Cells;
                //清除自动生成的表头
                tcHeader.Clear();


                //新添加的第一个表头单元格, 设置为合并17个列, 从而形成一行.
                tcHeader.Add(new TableHeaderCell());
                tcHeader[0].Text = "<tr><th>存货大类";
                tcHeader[0].RowSpan = 2;
                tcHeader.Add(new TableHeaderCell());
                tcHeader[1].Text = "<tr><th>存货大类";
                tcHeader[1].RowSpan = 2;
                tcHeader.Add(new TableHeaderCell());
                tcHeader[2].Text = "<tr><th>存货大类";
                tcHeader[2].RowSpan = 2;

                tcHeader.Add(new TableHeaderCell());
                tcHeader[3].Text = "存货编号";
                tcHeader[3].RowSpan = 2;
                tcHeader.Add(new TableHeaderCell());
                tcHeader[4].Text = "存货名称";
                tcHeader[4].RowSpan = 2;
                tcHeader.Add(new TableHeaderCell());
                tcHeader[5].Text = "存货规格";
                tcHeader[5].RowSpan = 2;

                tcHeader.Add(new TableHeaderCell());
                tcHeader[6].ColumnSpan = 5;
                tcHeader[6].Text = "初始成本";

                tcHeader.Add(new TableHeaderCell());
                tcHeader[7].Text = "最低成本";
                tcHeader[7].RowSpan = 2;

                tcHeader.Add(new TableHeaderCell());
                tcHeader[8].ColumnSpan = 2;
                tcHeader[8].Text = "1月份";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[9].ColumnSpan = 2;
                tcHeader[9].Text = "2月份";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[10].ColumnSpan = 2;
                tcHeader[10].Text = "3月份";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[11].ColumnSpan = 2;
                tcHeader[11].Text = "4月份";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[12].ColumnSpan = 2;
                tcHeader[12].Text = "5月份";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[13].ColumnSpan = 2;
                tcHeader[13].Text = "6月份";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[14].ColumnSpan = 2;
                tcHeader[14].Text = "7月份";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[15].ColumnSpan = 2;
                tcHeader[15].Text = "8月份";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[16].ColumnSpan = 2;
                tcHeader[16].Text = "9月份";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[17].ColumnSpan = 2;
                tcHeader[17].Text = "10月份";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[18].ColumnSpan = 2;
                tcHeader[18].Text = "11月份";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[19].ColumnSpan = 2;
                tcHeader[19].Text = "12月份</th></tr><tr>";

                //</th>表示当前单元格结束, </tr>表示本行结束, <tr>另起新一行    关键点

                //添加第三行所有的单元格
                tcHeader.Add(new TableHeaderCell());
                tcHeader[20].Text = "润达";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[21].Text = "康祥";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[22].Text = "益信";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[23].Text = "惠中";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[24].Text = "华臣";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[25].Text = "调整成本";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[26].Text = "应用成本";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[27].Text = "调整成本";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[28].Text = "应用成本";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[29].Text = "调整成本";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[30].Text = "应用成本";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[31].Text = "调整成本";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[32].Text = "应用成本";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[33].Text = "调整成本";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[34].Text = "应用成本";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[35].Text = "调整成本";
                tcHeader.Add(new TableHeaderCell());
                /*
                tcHeader[36].Text = "应用成本";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[37].Text = "调整成本";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[36].Text = "应用成本";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[37].Text = "调整成本";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[38].Text = "应用成本";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[39].Text = "调整成本";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[40].Text = "应用成本";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[41].Text = "调整成本";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[42].Text = "应用成本";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[43].Text = "调整成本";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[44].Text = "应用成本";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[45].Text = "调整成本";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[46].Text = "应用成本</th></tr>";
                 * */
               
            }
        }

        


    }
}
